//
//  SCJMulateChooseView.m
//  messageManager
//
//  Created by desunire on 2017/9/7.
//  Copyright © 2017年 desunire. All rights reserved.
//

#import "SCJMulateChooseView.h"

@implementation SCJMulateChooseView
{
    UILabel  *_title;//title
    UIView      * _listView;    // 下拉列表背景View
    UITableView * _tableView;   // 下拉列表
    NSMutableArray     * _titleArr;    // 选项数组
    NSMutableDictionary     * _titleDic;    // 选项字典
    CGFloat       _rowHeight;   // 下拉列表行高
    UIButton *_sureBtn;
}
-(instancetype)initWithFrame:(CGRect)frame andData:(NSMutableDictionary *)data andTitle:(NSArray *)array{
    //前提必须是全屏的的的
    self = [super initWithFrame:frame];
    if (self) {
        _rowHeight = 44;
        _titleDic = [NSMutableDictionary dictionaryWithDictionary:data];
        _titleArr = [NSMutableArray array];
         NSArray *keysArr = [_titleDic allKeys];
         for (int j=0; j<[_titleDic.allKeys count]; j++) {
                for (int i=0; i<[_titleDic.allKeys count]; i++) {
                    NSString *key =[keysArr objectAtIndex:i];
                    //NSString *value = [_titleDic valueForKey:key];
//                    if ([key isEqualToString:@"all"]&&j==0) {
//                        [_titleArr addObject:@"全部"];
//                    }
//                    if ([key isEqualToString:@"critical"]&&j==1) {
//                        [_titleArr addObject:@"严重"];
//                    }
//                    if ([key isEqualToString:@"major"]&&j==2) {
//                        [_titleArr addObject:@"重大"];
//                    }
//                    if ([key isEqualToString:@"minor"]&&j==3) {
//                        [_titleArr addObject:@"轻微"];
//                    }
//                    if ([key isEqualToString:@"warning"]&&j==4) {
//                        [_titleArr addObject:@"警告"];
//                    }
//                    if ([key isEqualToString:@"normal"]&&j==5) {
//                        [_titleArr addObject:@"正常"];
//                    }
//                    if ([key isEqualToString:@"unknown"]&&j==6) {
//                        [_titleArr addObject:@"未知"];
//                    }
                    if ([key isEqualToString:@"critical"]&&j==0) {
                        [_titleArr addObject:@"严重"];
                    }
                    if ([key isEqualToString:@"major"]&&j==1) {
                        [_titleArr addObject:@"重大"];
                    }
                    if ([key isEqualToString:@"minor"]&&j==2) {
                        [_titleArr addObject:@"轻微"];
                    }
                    if ([key isEqualToString:@"warning"]&&j==3) {
                        [_titleArr addObject:@"警告"];
                    }
                    if ([key isEqualToString:@"normal"]&&j==4) {
                        [_titleArr addObject:@"正常"];
                    }
                    if ([key isEqualToString:@"unknown"]&&j==5) {
                        [_titleArr addObject:@"未知"];
                    }
                }
         }
        [self setUI];
    }
    return self;
}


-(void)setUI{
        UIView *backView = [[UIView alloc] initWithFrame:self.frame];
        backView.backgroundColor = [UIColor blackColor];
        backView.alpha=0.3;
        [self addSubview:backView];
    
    // 下拉列表背景View
    _listView = [[UIView alloc] init];
    _listView.frame = CGRectMake(30, 40+64,self.frame.size.width-60,_titleArr.count*_rowHeight+50+100);
    _listView.clipsToBounds       = YES;
    _listView.layer.masksToBounds = YES;
    _listView.backgroundColor=[UIColor whiteColor];
    _listView.layer.cornerRadius = 5;
    [self addSubview:_listView];
    
    
    //topView
    
    UIView *topView =[UIView new];
    
    topView.frame = CGRectMake(0, 0, _listView.frame.size.width, 50);
    
    topView.backgroundColor = navColor;
    
    [_listView addSubview:topView];
    
    
    _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, _listView.frame.size.width, 30)];
    
    _title.text = @"选择告警级别";
    
    _title.textColor = [UIColor whiteColor];
    
    _title.textAlignment =NSTextAlignmentCenter;
    
    _title.font = [UIFont boldSystemFontOfSize:18.0];
    
    [_listView addSubview:_title];
    
    // 下拉列表TableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50,_listView.frame.size.width-20,_titleArr.count*_rowHeight)];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    _tableView.bounces         = NO;
    [_listView addSubview:_tableView];
    
    //下拉显示的确定按钮
    _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sureBtn setFrame:CGRectMake(40, _titleArr.count*_rowHeight+50+40, _listView.frame.size.width-80, 50)];
    [_sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_sureBtn addTarget:self action:@selector(sureMainBtn:) forControlEvents:UIControlEventTouchUpInside];
    _sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    _sureBtn.layer.masksToBounds = YES;
    _sureBtn.layer.cornerRadius = 5;
    _sureBtn.backgroundColor    = UIColorFromRGB(0xf2f2f2);
    
    [_listView addSubview:_sureBtn];
}


-(void)sureMainBtn:(UIButton *)button{
 
    
    if ([self.delegate respondsToSelector:@selector(selectedCellIndex:)]) {
        [self.delegate selectedCellIndex:_titleDic];
    }
    
}

#pragma mark - UITableView Delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _rowHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_titleDic allKeys].count;//[_titleArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    //= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // if (cell == nil) {
    //---------------------------下拉选项样式，可在此处自定义-------------------------
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = [_titleArr objectAtIndex:indexPath.row];
    title.frame          = CGRectMake(60, 0, _listView.frame.size.width-50, _rowHeight);
    title.font          = [UIFont systemFontOfSize:14.f];
    title.textColor     = [UIColor blackColor];
    cell.selectionStyle          = UITableViewCellSelectionStyleNone;
    
    [cell.contentView addSubview:title];
    
    UIImageView *image = [[UIImageView alloc] init];
    
    if ([self isChoose:indexPath.row]) {
        image.image=[UIImage imageNamed:@"icon9"];
    }else{
        image.image=[UIImage imageNamed:@"icon10"];
    }
    
    image.frame = CGRectMake(10, (_rowHeight-20)/2, 20, 20);
    [cell.contentView addSubview:image];
    
    [self cellAddImage:indexPath.row andCell:cell];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, _rowHeight -0.5, _listView.frame.size.width, 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [cell addSubview:line];
    //---------------------------------------------------------------------------
    // }
    return cell;
}
//每个cell添加一个imageView
-(void)cellAddImage:(NSInteger)index andCell:(UITableViewCell *)cell{
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(35,(_rowHeight-20)/2, 20, 20)];
    
    if (index == 0) {
        image.image=[UIImage imageNamed:@"critical"];
    }
    if (index == 1) {
        image.image=[UIImage imageNamed:@"major"];
    }
    if (index == 2) {
        image.image=[UIImage imageNamed:@"minor"];
    }
    if (index == 3) {
        image.image=[UIImage imageNamed:@"warning"];
    }
    if (index == 4) {
        image.image=[UIImage imageNamed:@"normal"];
    }
    if (index == 5) {
        image.image=[UIImage imageNamed:@"unknown"];
    }
    
    [cell.contentView addSubview:image];
    
}

//判断具体某一行是否选中了是否选中了
-(BOOL)isChoose:(NSInteger )indexpath{
//    if (indexpath==0) {
//        return [[_titleDic valueForKey:@"all"] intValue];
//    } else
    if (indexpath==0){
        return [[_titleDic valueForKey:@"critical"] intValue];
    } else if (indexpath==1){
        return [[_titleDic valueForKey:@"major"] intValue];
    }else if (indexpath==2){
        return [[_titleDic valueForKey:@"minor"] intValue];
    }else if (indexpath==3){
        return [[_titleDic valueForKey:@"warning"] intValue];
    }else if (indexpath==4){
        return [[_titleDic valueForKey:@"normal"] intValue];
    }else if (indexpath==5){
        return [[_titleDic valueForKey:@"unknown"] intValue];
    }
    return false;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//    if (indexPath.row==0) {
//        if ([[_titleDic valueForKey:@"all"] intValue]==1) {
//            [_titleDic setObject:@"0" forKey:@"all"];
//        }else{
//            [_titleDic setObject:@"1" forKey:@"all"];
//        }
//    } else
    if (indexPath.row==0){
        if ([[_titleDic valueForKey:@"critical"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"critical"];
        }else{
            [_titleDic setObject:@"1" forKey:@"critical"];
        }
    } else if (indexPath.row==1){
        if ([[_titleDic valueForKey:@"major"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"major"];
        }else{
            [_titleDic setObject:@"1" forKey:@"major"];
        }
    }else if (indexPath.row==2){
        if ([[_titleDic valueForKey:@"minor"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"minor"];
        }else{
            [_titleDic setObject:@"1" forKey:@"minor"];
        }
    }else if (indexPath.row==3){
        if ([[_titleDic valueForKey:@"warning"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"warning"];
        }else{
            [_titleDic setObject:@"1" forKey:@"warning"];
        }
    }else if (indexPath.row==4){
        if ([[_titleDic valueForKey:@"normal"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"normal"];
        }else{
            [_titleDic setObject:@"1" forKey:@"normal"];
        }
    }else if (indexPath.row==5){
        if ([[_titleDic valueForKey:@"unknown"] intValue]==1) {
            [_titleDic setObject:@"0" forKey:@"unknown"];
        }else{
            [_titleDic setObject:@"1" forKey:@"unknown"];
        }
    }
    
    [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section], nil] withRowAnimation:UITableViewRowAnimationNone];
    //[_tableView reloadData];
    
}


@end
